package com.mql.ganio.ui;

import android.media.MediaPlayer;
import android.net.Uri;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.mql.ganio.R;
import com.mql.ganio.ui.base.BaseActivity;
import com.mql.ganio.ui.presenter.LoginAtPresenter;
import com.mql.ganio.ui.view.ILoginAtView;
import com.mql.ganio.util.UIUtils;
import com.mql.ganio.widget.CustomVideoView;

import butterknife.Bind;

public class LoginActivity extends BaseActivity<ILoginAtView, LoginAtPresenter> implements ILoginAtView, View.OnClickListener {
    @Bind(R.id.videoview)
    CustomVideoView mVideoview;
    @Bind(R.id.ibAddMenu)
    ImageButton mIbAddMenu;
    @Bind(R.id.etPhone)
    EditText etPhone;
    @Bind(R.id.vLinePhone)
    View vLinePhone;
    @Bind(R.id.etPwd)
    EditText etPwd;
    @Bind(R.id.vLinePwd)
    View vLinePwd;
    @Bind(R.id.btnLogin)
    Button btnLogin;


    TextWatcher watcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            btnLogin.setEnabled(canLogin());
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    };

    @Override
    protected boolean isToolbarCanBack() {
        return false;
    }

    @Override
    public void initView() {
        mIbAddMenu.setVisibility(View.GONE);
        mVideoview.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.sport));
        //播放
        mVideoview.start();

    }


    @Override
    public void initListener() {
        etPwd.addTextChangedListener(watcher);
        etPhone.addTextChangedListener(watcher);
        etPwd.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    vLinePwd.setBackgroundColor(UIUtils.getColor(R.color.colorPrimary));
                } else {
                    vLinePwd.setBackgroundColor(UIUtils.getColor(R.color.line));
                }
            }
        });

        etPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    vLinePhone.setBackgroundColor(UIUtils.getColor(R.color.colorPrimary));
                } else {
                    vLinePhone.setBackgroundColor(UIUtils.getColor(R.color.line));
                }
            }
        });

        btnLogin.setOnClickListener(this);
        //循环播放
        mVideoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                mVideoview.start();
            }
        });
    }

    private boolean canLogin() {
        int pwdLength = etPwd.getText().toString().trim().length();
        int phoneLength = etPhone.getText().toString().trim().length();
        if (pwdLength > 0 && phoneLength > 0) {
            return true;
        }
        return false;
    }


    @Override
    protected LoginAtPresenter createPresenter() {
        return new LoginAtPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_login;
    }


    @Override
    public EditText getPhone() {
        return etPhone;
    }

    @Override
    public EditText getPassWord() {
        return etPwd;
    }

    @Override
    public void onClick(View v) {
        mPresenter.login();
    }


    int mPlayingPos = 0;

    @Override
    protected void onPause() {
        mPlayingPos = mVideoview.getCurrentPosition(); //先获取再stopPlay(),原因自己看源码
        mVideoview.stopPlayback();
        super.onPause();
    }

    @Override
    protected void onResume() {
        if (mPlayingPos > 0) {
            //此处为更好的用户体验,可添加一个progressBar,有些客户端会在这个过程中隐藏底下控制栏,这方法也不错
            mVideoview.start();
            mVideoview.seekTo(mPlayingPos);
            mPlayingPos = 0;
        }
        super.onResume();
    }

}
